<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="用户名称" name="userName">
            <t-input
              v-model="queryParams.userName"
              clearable
              placeholder="请输入用户名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="手机号码" name="phonenumber">
            <t-input
              v-model="queryParams.phonenumber"
              clearable
              placeholder="请输入手机号码"
              style="width: 240px"
            />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.roleId=queryParams.roleId;dialog.open_add_role_user = true;" variant="outline" theme="primary">
        <icon name="add" slot="icon"></icon>
        新增
      </t-button>
      <t-button  variant="outline"  @click="cancelAuthUserAll" :disabled="multiple" theme="danger">
        <icon name="close-circle" slot="icon"></icon>
        批量取消授权
      </t-button>
      <t-button  variant="outline" @click="handleClose" theme="warning">
        <icon name="close" slot="icon"></icon>
        关闭
      </t-button>
    </div>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      @select-change="rehandleSelectChange"
      hover
      :pagination="pagination"
      :loading="dataLoading"
      :selected-row-keys.sync="userIds"
    >
      <template #status="{ row }">
        <t-tag v-if="row.status === '0'" theme="success" variant="light">正常</t-tag>
        <t-tag v-else theme="danger" variant="light">停用</t-tag>
      </template>
      <template #op="{ row }">
        <t-button @click="cancelUserRole(row)" size="small" variant="text" class="t-button-link">
          <icon name="close-circle" slot="icon"/>
          取消授权
        </t-button>
      </template>
    </t-table>
    <add-role-user @reload="getList()" :visible.sync="dialog.open_add_role_user" :roleId="dialog.roleId" v-if="dialog.open_add_role_user"></add-role-user>
  </div>
</template>

<script>
import {Icon} from 'tdesign-icons-vue';
import AddRoleUser from './components/add-role-user.vue'
import { allocatedUserList, authUserCancel, authUserCancelAll } from "@/api/system/role";

export default {
  name: "role-user-index",
  components:{
    Icon,
    AddRoleUser
  },
  data(){
    return {
      userIds: [],
      data:[],
      multiple: true,
      columns:[
        {
          colKey: 'row-select',
          type: 'multiple',
          width:55,
        },
        {
          colKey: "userName",
          title: "登录名称",
          align: "center"
        },
        {
          colKey: "nickName",
          title: "真实姓名",
          align: "center"
        },
        {
          colKey: "email",
          title: "邮箱",
          align: "center"
        },
        {
          colKey: "phonenumber",
          title: "手机",
          align: "center"
        },
        {
          colKey: "status",
          title: "状态",
          align: "center"
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center"
        },
        {
          colKey: "op",
          title: "操作",
          align: "center"
        },
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      dialog:{
        open_add_role_user: false,
        roleId: 0,
      }
    }
  },
  created() {
    const roleId = this.$route.params && this.$route.params.roleId;
    if (roleId) {
      this.queryParams.roleId = roleId;
      this.getList();
    }
  },
  methods:{
    /** 查询授权用户列表 */
    getList() {
      this.dataLoading = true;
      allocatedUserList(this.queryParams).then(response => {
        this.dataLoading = false;
        this.data = response.rows;
        this.pagination.total = response.total;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    cancelUserRole(row){
      const confirmDia = this.$dialog.confirm({
        header:"系统提示",
        body:`确认要取消该用户"${row.userName}"角色吗？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          authUserCancel({ userId: row.id, roleId: this.queryParams.roleId }).then(
            (res) => {
              this.getList();
              this.$message.success("取消授权成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    },
    cancelAuthUserAll(){
      const confirmDia = this.$dialog.confirm({
        header:"系统提示",
        body:`是否取消选中用户授权数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          authUserCancelAll({ roleId: this.queryParams.roleId, userIds: this.userIds.join(",") }).then(
            (res) => {
              this.getList();
              this.$message.success("取消授权成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    },
    onSubmit(){
      this.getList();
    },
    handleClose() {
      const obj = { path: "/home/system/role" };
      this.$tab.closeOpenPage(obj);
    },
    rehandleSelectChange(value, { selectedRowData }){
      this.multiple = !value.length;
    }
  }
}
</script>

<style scoped>

</style>
